//
//  AppDelegate.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/3/29.
//
import UMCommon
import ThingSmartBaseKit
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate {
    var window: UIWindow?
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        Task {
            await _initializeAsyncSDKs()
        }
        return true
    }
    

    // MARK: - initialize SDKs
    private func _initializeAsyncSDKs() async {
        await withTaskGroup(of: Void.self) { group in
            //
            group.addTask {
                await self._initializeTuyaSDK()
            }
            group.addTask {
                await self._initializeTXSDK()
            }
            group.addTask {
                await self._initializeWXSDK()
            }
            group.addTask {
                await self._initializeUMSDK()
            }
        }
    }

    /// 初始化TuyaSDK
    private func _initializeTuyaSDK() {
        ThingSmartSDK.sharedInstance().start(withAppKey: ThirdParty.Tuya_appKey, secretKey: ThirdParty.Tuya_appSecret)
    #if DEBUG
        ThingSmartSDK.sharedInstance().debugMode = true
    #endif
    }

    /// 初始化TXSDK
    private func _initializeTXSDK() {
        TXCommonHandler.sharedInstance().setAuthSDKInfo(ThirdParty.TX_appSecret) { resultDic in
            print(resultDic)
        }
    }
    
    // 初始化微信sdk
    private func _initializeWXSDK() {
        #if DEBUG
            WXApi.startLog(by: WXLogLevel.detail, logBlock: { log in
                Logger.log(.info, message:"\(log)", tag: LogTag.wechat)
            })
        #endif
        WXApi.registerApp(ThirdParty.WX_appId, universalLink: ThirdParty.WX_Link)
    }
    
    // 初始化友盟sdk
    private func _initializeUMSDK() {
        UMConfigure.initWithAppkey(ThirdParty.UM_appKey, channel: "App Store")
        UMConfigure.setLogEnabled(false)
#if DEBUG
        UMConfigure.setLogEnabled(false)
#endif
    }
}
